package com.liuxvru.project.system.controller;

import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.core.page.TableDataInfo;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.common.utils.poi.ExcelUtil;
import com.liuxvru.project.domain.Work;
import com.liuxvru.project.service.IWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工作项Controller
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/project/work")
public class WorkController extends BaseController {
    @Autowired
    private IWorkService workService;

    /**
     * 查询工作项列表
     */
    @PreAuthorize("@ss.hasPermi('project:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(Work work) {
        startPage();
        List<Work> list = workService.selectWorkList(work);
        return getDataTable(list);
    }

    /**
     * 导出工作项列表
     */
    @PreAuthorize("@ss.hasPermi('project:work:export')")
    @Log(title = "工作项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Work work) {
        List<Work> list = workService.selectWorkList(work);
        ExcelUtil<Work> util = new ExcelUtil<Work>(Work.class);
        util.exportExcel(response, list, "工作项数据");
    }

    /**
     * 获取工作项详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:work:query')")
    @GetMapping(value = "/{workItemId}")
    public AjaxResult getInfo(@PathVariable("workItemId") Long workItemId) {
        return success(workService.getById(workItemId));
    }

    /**
     * 新增工作项
     */
    @PreAuthorize("@ss.hasPermi('project:work:add')")
    @Log(title = "工作项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Work work) {
        return toAjax(workService.save(work));
    }

    /**
     * 修改工作项
     */
    @PreAuthorize("@ss.hasPermi('project:work:edit')")
    @Log(title = "工作项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Work work) {
        return toAjax(workService.updateById(work));
    }

    /**
     * 删除工作项
     */
    @PreAuthorize("@ss.hasPermi('project:work:remove')")
    @Log(title = "工作项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workItemIds}")
    public AjaxResult remove(@PathVariable Long[] workItemIds) {
        return toAjax(workService.removeById(workItemIds));
    }
}
